<?php

if (!defined("IN_OP"))
{
    exit("You can't access this file directly...");
}
require_once( "class_3d/CheckMode_3d.php" );
$gametype = intval(getvar("gametype"));
$chk = new CheckMode_3d($curruser['acl'], 1, $gametype);
$chk->checkShow3dPage();
$show3D3S = $chk->show3D3S;
unset($chk);

if ($curruser['acl'] != "255")
{
    exit();
}
$tbl = $g_gametype_3d[$gametype]['en'];
$tblName = $g_gametype_3d[$gametype]['ch'];
$cmd = getvar("cmd");
$conf = new Config();

if ($cmd == "stop")
{
    switch ($show3D3S)
    {
        case 0 :
            $stopTime = date("Hi");
            $conf->set("stopbet_{$tbl}", $stopTime);
            header("Cache-Control: no-cache, must-revalidate");
            header("Content-Type: text/plain");
            echo "true";
            break;
        case 1 :
        case 2 :
		case 3 :
            $stopTime = date("Y-m-d H:i:s");
            $fields = array();
            $fields['stopbet'] = "'" . $stopTime . "'";
            $id = getvar("id");
            $echoStr = "";
            if ($id != "")
            {
                db_update("lotto_{$tbl}", $fields, "id={$id} AND openbet > '{$stopTime}' ");
                $echoStr = "true";
            }
            else
            {
                $echoStr = "false";
            }
            header("Cache-Control: no-cache, must-revalidate");
            header("Content-Type: text/plain");
            echo $echoStr;
            break;
    }
}
else if ($cmd == "fly")
{
    $conf->set("protect_start_{$tbl}", "1");
    header("Cache-Control: no-cache, must-revalidate");
    header("Content-Type: text/plain");
    echo "true";
}
else if ($cmd == "calc")
{
    $chkGametype = chkGametype($gametype);

    if ($chkGametype < 1)
    {
        exit();
    }
    $id = intval(getvar("id"));
    if (!$id)
    {
        exit();
    }

    if ($chkGametype == 3)
    {
        include_once( "class_5h/class.lotto5h.php" );
        $lotto3d = new lotto5h($gametype);
    }
	elseif ($chkGametype == 4)
    {
        include_once( "class_sb/class.lottosb.php" );
        $lotto3d = new lottosb($gametype);
    }
    else
    {
        include_once( "class_3d/class.lotto3d.php" );
        $lotto3d = new lotto3d($gametype);
    }
    $fields = array();
    $fields['state'] = "1";
    $conf->set("calcbet_{$tbl}", "1");

    switch ($chkGametype)
    {
        case 1 :
            $stopTime = date("Hi");
            break;
        case 2 :
        case 3 :
		case 4 :
            $fields['calcbet'] = "1";
            break;
        default :
            exit();
            break;
    }
    db_update("lotto_{$tbl}", $fields, "id={$id}");
    $postTime = $conf->get("paper_post_bet");
    $today = date("Hi") <= $postTime ? date("Y-m-d", strtotime("-1 day")) : date("Y-m-d");
    $lotto_id = $lotto3d->CheckLottoOpen($id);
    if ($lotto_id == $id)
    {
        $x = $lotto3d->openDate == $today ? "" : "x";
        $lotto3d->OpenProcess($lotto_id, $x);
        if ($gametype == 12)
        {
            $sql = "SELECT open FROM lotto_3s3 WHERE id = {$lotto_id} LIMIT 1";
            $rs = getdb($sql);
            if (!$rs->eof)
            {
                $sql = "SELECT id FROM lotto_3s3 WHERE open = '{$rs->f[0]}' ORDER BY openbet DESC LIMIT 1";
                $rs = getdb($sql);
                if (!$rs->eof && $rs->f[0] == $lotto_id)
                {
                    include_once( "class_3d/Amounts_float_3d.php" );
                    $AmountFloat3D = new Amounts_float_3d();
                    $AmountFloat3D->countBet($lotto_id, "3s3", $x);
                }
            }
        }
        if (in_array($chkGametype, array(2, 3)))
        {
            db_update("lotto_{$tbl}", array("calcbet" => "0"), "id={$id}");
        }
    }
    header("Cache-Control: no-cache, must-revalidate");
    //header("Content-Type: text/plain");
    echo "true";
}
else if ($cmd == "openDisCalc")
{
    if ($curruser['account'] == G_SUPERACCOUNT && $curruser['acl'] == 255 && 2 < $gametype)
    {
        $id = intval(getvar("id"));
        $sql = "UPDATE lotto_{$tbl} SET calcbet = 0 WHERE id ={$id}";
        getdb($sql);
        header("Cache-Control: no-cache, must-revalidate");
        header("Content-Type: text/plain");
        echo "true";
        exit();
    }
}
else if ($cmd == "editNote")
{
    if ($curruser['acl'] == 255 && $curruser['prev_id'] == 0)
    {
        $id = intval(getvar("id"));
        $note = getvar("iptNote");
        if ($note != "")
        {
            if ($g_conf['language'] == "zh-cn")
            {
                $note = iconv("UTF-8", "GB2312", $note);
                include_once( "class/ccharset.php" );
                $cc = new CCharset();
                $note = strip_tags($note);
                $note = $cc->Gb_Big5($note);
                $note = htmlspecialchars($note, ENT_QUOTES);
                header("Content-type: text/plain; charset=utf-8");
            }
            else
            {
                $note = iconv("UTF-8", "big5", $note);
                $note = htmlspecialchars($note, ENT_QUOTES);
                header("Content-type: text/plain; charset=GB2312");
            }
            $sql = "UPDATE lotto_{$tbl} SET note = '{$note}' WHERE id ={$id}";
            getdb($sql);
            header("Cache-Control: no-cache, must-revalidate");
            echo "true";
        }
        else
        {
            //header("Content-type: text/plain;");
            header("Cache-Control: no-cache, must-revalidate");
            echo "false";
        }
        exit();
        exit();
    }
}
else if ($cmd == "getNote")
{
    $id = intval(getvar("id"));
    $sql = "SELECT note FROM lotto_{$tbl} WHERE id ={$id} LIMIT 1";
    $rs = getdb($sql);
    $note = $rs->eof ? "" : $rs->f[0];
    if ($note != "" && $g_conf['language'] == "zh-cn")
    {
        include_once( "class/ccharset.php" );
        $cc = new CCharset();
        $note = $cc->Big5_Gb($note);
        header("Content-type: text/html; charset=GB2312");
    }
    else
    {
        header("Content-type: text/html; charset=utf-8");
    }
    header("Cache-Control: no-cache, must-revalidate");
    echo $note;
    exit();
}
else
{
    if ($cmd == "chgMyCssChange")
    {
        $conf->set("myCssChange_3d", intval(getvar("sentVal")));
        header("Cache-Control: no-cache, must-revalidate");
        header("Content-Type: text/plain");
        echo "true";
        exit();
    }
    else
    {
        if ($cmd == "chgTime")
        {
            if ($gametype != 4)
            {
                exit();
            }
            if ($conf->get("systemcheck_{$tbl}") != 1)
            {
                exit();
            }
            $conf->set("lotto_auto_{$tbl}", 0);
            $id = intval(getvar("id"));
            $sql = "!!SELECT startbet, `open` FROM lotto_{$tbl} WHERE id ={$id} LIMIT 1";
            $rs = getdb($sql);
            if ($rs->eof)
            {
                exit();
            }
            $open = $rs->f['open'];
            $startbet = $rs->f['startbet'];
            $addMin = max(0, intval(getvar("addMin")));
            $sql = "UPDATE lotto_{$tbl}";
            $sql .= " SET `startbet`= date_add(`startbet`,interval {$addMin} minute)";
            $sql .= " ,`stopbet`= date_add(`stopbet`,interval {$addMin} minute)";
            $sql .= " ,`openbet`= date_add(`openbet`,interval {$addMin} minute)";
            $sql .= " WHERE `open` = '{$open}' AND startbet >= '{$startbet}'";
            getdb($sql);
            getdb("INSERT INTO `dblog` (`logs`) VALUES ('[暂放记录]延後时间,时间:{$addMin},ID:" . $curruser['id'] . ",open:{$open},lottoid:{$id},st:{$startbet}')");
            header("Cache-Control: no-cache, must-revalidate");
            header("Content-Type: text/plain");
            echo "true";
            exit();
        }
    }
}
?>